﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DusonFramework.DependenceInjection
{
    /// <summary>
    /// The base class of container context
    /// </summary>
    public abstract class ContainerContextBase : IContainerContext
    {
        #region Fields

        /// <summary>
        /// The name of container
        /// </summary>
        private string _name = string.Empty;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="ContainerContextBase"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        public ContainerContextBase(string name)
        {
            this._name = name;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get
            {
                return _name;
            }
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs before container initialized.
        /// </summary>
        public event EventHandler<ContainerEventArgs> BeforeInitialized;

        /// <summary>
        /// Occures after container initialized
        /// </summary>
        public event EventHandler<ContainerEventArgs> AfterInitialized;

        /// <summary>
        /// Occures before container teardown
        /// </summary>
        public event EventHandler<ContainerEventArgs> BeforeTearDown;

        /// <summary>
        /// Occures after container teardown
        /// </summary>
        public event EventHandler<ContainerEventArgs> AfterTearDown;

        /// <summary>
        /// Occures when container resolve failed
        /// </summary>
        public event EventHandler<ContainerFailedEventArgs> ResolveFailed;

        #endregion

        #region Protected virtual Methods

        /// <summary>
        /// Raises the <see cref="E:BeforeInitialized"/> event.
        /// </summary>
        /// <param name="e">The <see cref="ContainerEventArgs"/> instance containing the event data.</param>
        protected virtual void OnBeforeInitialized(ContainerEventArgs e)
        {
            EventHandler<ContainerEventArgs> tempHandler = BeforeInitialized;
            if (tempHandler != null)
            {
                BeforeInitialized(this, e);
            }
        }

        /// <summary>
        /// Raises the <see cref="E:AfterInitialized"/> event.
        /// </summary>
        /// <param name="e">The <see cref="ContainerEventArgs"/> instance containing the event data.</param>
        protected virtual void OnAfterInitialized(ContainerEventArgs e)
        {
            EventHandler<ContainerEventArgs> tempHandler = AfterInitialized;
            if (tempHandler != null)
            {
                AfterInitialized(this, e);
            }
        }

        /// <summary>
        /// Raises the <see cref="E:BeforeTearDown"/> event.
        /// </summary>
        /// <param name="e">The <see cref="ContainerEventArgs"/> instance containing the event data.</param>
        protected virtual void OnBeforeTearDown(ContainerEventArgs e)
        {
            EventHandler<ContainerEventArgs> tempHandler = BeforeTearDown;
            if (tempHandler != null)
            {
                BeforeTearDown(this, e);
            }
        }

        /// <summary>
        /// Raises the <see cref="E:AfterTearDown"/> event.
        /// </summary>
        /// <param name="e">The <see cref="ContainerEventArgs"/> instance containing the event data.</param>
        protected virtual void OnAfterTearDown(ContainerEventArgs e)
        {
            EventHandler<ContainerEventArgs> tempHandler = AfterTearDown;
            if (tempHandler != null)
            {
                AfterTearDown(this, e);
            }
        }

        /// <summary>
        /// Raises the <see cref="E:ResolveFailed"/> event.
        /// </summary>
        /// <param name="e">The <see cref="ContainerFailedEventArgs"/> instance containing the event data.</param>
        protected virtual void OnResolveFailed(ContainerFailedEventArgs e)
        {
            EventHandler<ContainerFailedEventArgs> tempHandler = ResolveFailed;
            if (tempHandler != null)
            {
                ResolveFailed(this, e);
            }
        }

        #endregion

        #region Abstract Methods

        /// <summary>
        /// Initializes container.
        /// </summary>
        public abstract void Initialize();

        /// <summary>
        /// Resolves special instance.
        /// </summary>
        /// <typeparam name="T">Parameter type</typeparam>
        /// <returns></returns>
        public abstract T Resolve<T>();

        /// <summary>
        /// Tears down.
        /// </summary>
        public abstract void TearDown();

        #endregion
    }
}
